Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
Evite que el div contenteditable="plaintext-only" cree una nueva línea

Tengo un div que se usa como entrada del usuario:
<div id="chat-message-input" contenteditable="plaintext-only">

Cuando el usuario presiona la tecla Intro (y no mantiene presionada la tecla Mayús), se envía el texto del usuario.

 <script> document.querySelector('#chat-message-input').onkeyup = function(e) { // User hits enter key and is not holding shift if (e.keyCode === 13 && event.shiftKey != 1) { //Click submit button document.querySelector('#chat-message-submit').click(); // Clear input field document.getElementById('chat-message-input').innerHTML=''; }; </script>

Problema: hay un breve momento en el que el div crea una nueva línea porque se presionó la tecla Intro, antes de que se borre el contenido del div.

Solo quiero que se cree una nueva línea en estas circunstancias:

  1. El texto de entrada del usuario llega al final de la línea.
  2. El usuario mantiene presionada la tecla shift y presiona enter

¿Alguien sabe cómo puedo evitar que se cree una nueva línea dados los requisitos anteriores?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Como sugirió A Haworth, también intercepté onkeydown (además de onkeyup) y evité el valor predeterminado. Esto pareció funcionar bien.

 document.querySelector('#chat-message-input').onkeydown = function(e) { // User hits enter key and is not holding shift if (e.keyCode === 13 && event.shiftKey != 1) { e.preventDefault() } };

Esto hizo que el campo de entrada fuera realmente sólido, lo cual es necesario para mi aplicación de chat en tiempo real. ¡Gracias!

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!